﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;
namespace TaxiDispatchClient
{
    public partial class TaxiGroupForm : DevExpress.XtraEditors.XtraForm
    {
        public TaxiGroupForm()
        {
            InitializeComponent();
            BindGrid(); 
        }

        private void BindGrid()
        {
            try
            {
                DataSet ds = MainForm.PositionService.TaxiGroupGetList("");
                gridControl1.DataSource = ds.Tables[0];
                DataRow row = this.gridView1.GetDataRow(gridView1.FocusedRowHandle);
                if (row != null)
                {
                    label_id.Text = row["id"].ToString();
                    textEdit_groupname.Text = row["TaxiGroup"].ToString();
                    memoEdit_remark.Text = row["Remark"].ToString();
                }
            }
            catch (Exception ex)
            {
              Common.CommonRoutine.HandlingExceptions(ex);
              return;
            }
            

        }
        
        private void simpleButton_add_Click(object sender, EventArgs e)
        {
           if(btn_add.Tag.ToString()=="新增")
           {
               gridControl1.Enabled = false;
               btn_add.Image = Properties.Resources.UserForm_btnsave001;
               btn_add.Tag = "保存";
               btn_cancel.Location = btn_modify.Location;
               btn_modify.Visible = false;
               btn_cancel.Visible = true;
               btn_delete.Visible = false;
               textEdit_groupname.Enabled = true;
               memoEdit_remark.Enabled = true;
               textEdit_groupname.Text = "";
               memoEdit_remark.Text = "";
               label_id.Text = "";
           }
            else
           {
               if(!CheckedPass())
                   return;
               string strTaxiGroup = textEdit_groupname.Text.ToString();
               string strRemark = memoEdit_remark.Text.ToString();
               TaxiGroupInfo taxiGroupInfo = new TaxiGroupInfo {TaxiGroup = strTaxiGroup, Remark = strRemark};
              try
              {
                  if(MainForm.PositionService.TaxiGroupExist(strTaxiGroup))
                  {
                      XtraMessageBox.Show("该分组已存在，请重新命名！");
                      return;
                  }
                  if (MainForm.PositionService.TaxiGroupAdd(taxiGroupInfo))
                  {
                      XtraMessageBox.Show("添加成功！");                                           
                      //DataRow row = gridView1.GetDataRow(gridView1.DataRowCount - 1);
                      //label_id.Text = row["id"].ToString();
                     
                      
                      DataSet ds = MainForm.PositionService.TaxiGroupGetList("");
                      gridControl1.DataSource = ds.Tables[0];
                      gridView1.FocusedRowHandle = gridView1.RowCount - 1;
                      DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle);
                      if (row != null)
                      {
                          label_id.Text = row["id"].ToString();                         
                      }
                       
                  }
                  else
                  {
                      XtraMessageBox.Show("添加失败！");
                      
                  }

                  ResetControl();
                  
              }
              catch(Exception ex)
              { 
                  Common.CommonRoutine.HandlingExceptions(ex,true);
                  return;
              }
           }
        }
        private void ResetControl()
        {
            gridControl1.Enabled = true;
            textEdit_groupname.Enabled = false;
            memoEdit_remark.Enabled = false;
            btn_add.Tag = "新增";
            btn_add.Image = Properties.Resources.UserForm_btnadd001;
            btn_add.Visible = true;
            btn_modify.Visible = true;
            btn_cancel.Visible = false;
            btn_delete.Visible = true;
            btn_modify.Image = Properties.Resources.UserForm_btnmodify001;
            btn_modify.Tag = "修改";
            btn_modify.Location = btn_cancel.Location;

            DataRow row = this.gridView1.GetDataRow(gridView1.FocusedRowHandle);
            if (row != null)
            {
                label_id.Text = row["id"].ToString();
                textEdit_groupname.Text = row["TaxiGroup"].ToString();
                memoEdit_remark.Text = row["Remark"].ToString();
            }

        }
        private bool CheckedPass()
        {
            string strTaxiGroup = textEdit_groupname.Text;
            if(strTaxiGroup.Trim()=="")
            {
                XtraMessageBox.Show("分组名称不能为空！");
                textEdit_groupname.Focus();
                return false;
            }
            if(strTaxiGroup.Length>100)
            {
                XtraMessageBox.Show("分组名称字数过多，请不要超过100个字符，当前共用【" + strTaxiGroup.Length + "】字符");
                textEdit_groupname.Focus();
                return false;
            }
            if(memoEdit_remark.Text.Length>20)
            {
                XtraMessageBox.Show("备注内容过长，不能超过20字符，当前使用【"+memoEdit_remark.Text.Length+"】字符");
                memoEdit_remark.Focus();
                return false;
            }
            return true;
        }
        private void simpleButton_delete_Click(object sender, EventArgs e)
        {
            DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            if (row != null)
            {
              int id = int.Parse(row["id"].ToString());
              if (XtraMessageBox.Show("确认删除流水号为【"+id+"】的广告分组吗？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    
                    if (MainForm.PositionService.TaxiGroupDelete(id))
                    {
                        XtraMessageBox.Show("删除成功！");
                        textEdit_groupname.Text = "";
                        memoEdit_remark.Text = "";
                        label_id.Text = "";
                        BindGrid();
                    }
                    else
                        XtraMessageBox.Show("删除失败！");
                }
            }
            else
                XtraMessageBox.Show("请选择想要删除的一行数据！");
        }

        private string _taxiGroup;
        private void simpleButton_modify_Click(object sender, EventArgs e)
        {

            if (label_id.Text == "")
            {
                XtraMessageBox.Show("请选择想要修改的分组");
                return;
            }
            if(btn_modify.Tag.ToString() =="修改")
            {
                _taxiGroup = textEdit_groupname.Text;
                btn_modify.Image = Properties.Resources.UserForm_btnsave001;
                btn_modify.Tag = "保存";
                btn_add.Visible = false;
                btn_cancel.Location = btn_modify.Location;
                btn_modify.Location = btn_add.Location;
                btn_delete.Visible = false;
                btn_cancel.Visible = true;
                textEdit_groupname.Enabled = true;
                memoEdit_remark.Enabled = true;
                gridControl1.Enabled = false;
            }
            else
            {
                if (label_id.Text == "流水号")
                {
                    XtraMessageBox.Show("请选择想要修改的分组");
                    ResetControl();
                    return;
                }
                if (!CheckedPass())
                    return;
                int intId = int.Parse(label_id.Text);
                string strTaxiGroup = textEdit_groupname.Text;
                string strRemark = memoEdit_remark.Text;
                if(strTaxiGroup!=_taxiGroup)
                {
                    if (MainForm.PositionService.TaxiGroupExist(strTaxiGroup))
                    {
                        XtraMessageBox.Show("该分组已存在，请重新命名！");
                        return;
                    }
                }
                TaxiGroupInfo taxiGroupInfo = new TaxiGroupInfo 
                {Id = intId, TaxiGroup = strTaxiGroup, Remark = strRemark };
                if (MainForm.PositionService.TaxiGroupUpdate(taxiGroupInfo))
                {
                    XtraMessageBox.Show("修改成功！");
                    label_id.Text = "";
                    textEdit_groupname.Text = "";
                    memoEdit_remark.Text = "";

                }
                else
                    XtraMessageBox.Show("修改失败！");
                ResetControl();
                BindGrid();
            }
        }
        

        private void simpleButton_cancel_Click(object sender, EventArgs e)
        {
            ResetControl();
        }
       

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow row = this.gridView1.GetDataRow(gridView1.FocusedRowHandle);
            if(row!=null)
            {
                label_id.Text = row["id"].ToString();
                textEdit_groupname.Text = row["TaxiGroup"].ToString();
                memoEdit_remark.Text = row["Remark"].ToString();
            }
        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;
            Cursor = Cursors.Arrow;
        }
    }
}